package com.xexun.gpstracker.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.common.MyConstants;
import com.xexun.gpstracker.dialog.WizardAlertDialog;
import com.xexun.gpstracker.inter.HttpCallback;
import com.xexun.gpstracker.listener.PrettyTextChangeListener;
import com.xexun.gpstracker.pojo.ResponseData;
import com.xexun.gpstracker.pojo.User;
import com.xexun.gpstracker.util.CommUtil;
import com.xexun.gpstracker.util.HttpUtil;
import com.xexun.gpstracker.util.JsonHttpResponseHelper;
import com.xexun.gpstracker.util.Utils;
import com.xexun.gpstracker.vo.Session;
import org.json.JSONObject;

/**
 * 找回密码设置新密码页面
 *
 * @author huangfei
 */
public class UpdatePwdActivity extends Activity {

    private ImageButton backButton;
    private Button completeButton;
    private EditText pwdText, confirmPwdText, phoneText, oldPwdET;

    private String phone = "";
    private String code = "";
    private String imei = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);
//        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("validCode");
        imei = getIntent().getStringExtra("imei");

        backButton = (ImageButton) findViewById(R.id.backBtn);
        completeButton = (Button) findViewById(R.id.completeBtn);
        pwdText = (EditText) findViewById(R.id.pwdET);
        confirmPwdText = (EditText) findViewById(R.id.pwdConfirmET);
        phoneText = (EditText) findViewById(R.id.phoneET);
        oldPwdET = (EditText) findViewById(R.id.oldPwdET);

        backButton.setOnClickListener(clickListener);
        completeButton.setOnClickListener(clickListener);
        phoneText.addTextChangedListener(new PrettyTextChangeListener(3, 4));
    }


    private OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.backBtn:
                    UpdatePwdActivity.this.finish();
                    break;
                case R.id.completeBtn:
                    String pwd = pwdText.getText().toString().trim();
                    String oldpwd = oldPwdET.getText().toString().trim();
                    SharedPreferences mSharedPreferences = Utils.getPreferences(UpdatePwdActivity.this);
                    String savepwd = mSharedPreferences.getString(MyConstants.USER_PWD, "");
//                    phone = CommUtil.trimSeprator(phoneText.getText().toString().trim(), null);
                    String savedPhone = mSharedPreferences.getString(MyConstants.USER_PHONE, "");
                    if (CommUtil.isBlank(oldpwd)) {
                        CommUtil.showMsgShort(getString(R.string.pwd_null_err));
                        return;
                    }
                    if(!oldpwd.equals(savepwd)){
                        CommUtil.showMsgShort(getString(R.string.pwd_err));
                        return;
                    }
//                    if (Constants.IS_CHINA_VERSION && !Utils.isChinaMobileNO(phone)) {
//                        CommUtil.showMsgShort(getString(R.string.not_china_mobile));
//                        return;
//                    }
                    if (CommUtil.isBlank(pwd)) {
                        Toast.makeText(UpdatePwdActivity.this, R.string.pwd_null_err, Toast.LENGTH_SHORT).show();
                    } else if (pwd.length() < 6 || pwd.length() > 20) {
                        Toast.makeText(UpdatePwdActivity.this, R.string.pwd_length_err, Toast.LENGTH_SHORT).show();
                    } else if (!pwd.equals(confirmPwdText.getText().toString().trim())) {
                        Toast.makeText(UpdatePwdActivity.this, R.string.pwd_confirm_err, Toast.LENGTH_SHORT).show();
                    } else {
                        if (savedPhone != null) {
//                            updatePwd(phone, code, pwd, imei);
                            modifyPwd(savedPhone, oldpwd, pwd);
                        }

                    }
                    break;
            }
        }
    };


    /**
     * 获取验证码
     *
     * @param phoneStr
     */
    private void updatePwd(String phoneStr, String codeStr, String pwdStr, String imei) {
        User.modifyPassword(this, phoneStr, pwdStr, codeStr, imei, new HttpCallback() {
            @Override
            public void onStart() {
                WizardAlertDialog.getInstance().showProgressDialog(R.string.updating_pwd, UpdatePwdActivity.this);
            }

            @Override
            public void onFinish() {
                WizardAlertDialog.getInstance().closeProgressDialog();
            }

            @Override
            public void onSuccess(ResponseData obj) {
                if (obj.getRetcode() == 1) {
                    Toast.makeText(UpdatePwdActivity.this, R.string.pwd_setup_success, Toast.LENGTH_SHORT).show();
                    Session.getInstance().setNeedClose(true);
                    UpdatePwdActivity.this.finish();
                } else {
                    Toast.makeText(UpdatePwdActivity.this, obj.getMsg(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(String message) {
                Toast.makeText(UpdatePwdActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    static final String MODIFYPWDURL = Constants.baseUrl + "user/modifypasswd";





    private void modifyPwd(String account, String oldpwd, String pwd) {
        try {
            JSONObject json = new JSONObject();
            json.put("account", account);
            json.put("pwd", Utils.enctype(oldpwd));
            json.put("newpwd", Utils.enctype(pwd));
            Log.i("modifyPwd","account:"+account+"pwd:"+oldpwd+"newpwd"+pwd);
            HttpUtil.post(MODIFYPWDURL, json, new JsonHttpResponseHelper(new HttpCallback() {
                @Override
                public void onSuccess(ResponseData obj) throws Exception {
                    if (obj.getRetcode() == 1) {
                        CommUtil.showMsgShort(getString(R.string.modifypwd_success));

                        Intent intent = new Intent(UpdatePwdActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }else{
                        Toast.makeText(UpdatePwdActivity.this, obj.getMsg(), Toast.LENGTH_SHORT).show();
                    }



                }

                @Override
                public void onFailure(String message) throws Exception {
                    CommUtil.showMsgShort(getString(R.string.modifypwd_failure));
//                 Log.e("Modify","message:"+message);
                }
            }).getHandler());
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
            } catch (Exception e) {

            }
        }

    }
}